Vue.config.productionTip=false  //以阻止 vue 在启动时生成生产提示。 <!-- 全局配置 -->

const pas = (rule, value, callback) => {
    if (value === '') {
        callback(new Error('请再次输入密码'));
    } else if (value !== v.$data.formData.password1) {
        callback(new Error('两次输入密码不一致!'));
    } else {
        callback();
    }
};
const v=new Vue({
    el:'#app',
    data:{
        loading: false,    //页面加载,false:关闭,true:加载中
        //用户信息
        formData:{
            username:'',
            password1:'',
            password2:'',
            email: '',
        },
        //表单规则
        rules:{
            username: [{ required: true, message: '用户名不能为空', trigger: 'blur' },
                {min:2,max:80, message: '用户名不能小于2位,，大于80位', trigger: 'blur' },
            ],  //blur失去焦点
            password1: [{ required: true, message: '密码不能为空', trigger: 'blur'},
                { pattern: /^[ a-zA-Z0-9]{6,30}$/, message: '密码不能小于6位,不能大于30位,无特殊字符', trigger: 'blur'},
            ],
            password2: [{ required: true, message: '密码不能为空', trigger: 'blur'},
                {validator: pas,trigger: 'blur'}],
            email: [{ required: true, message: '邮箱不能为空', trigger: 'blur' },
                { pattern: /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/, message: '请输入正确的邮箱', trigger: 'blur' },
            ],  //blur失去焦点
        },
    },
    methods:{
        //注册
        login(){
            this.$refs.formData.validate((valid) => {
                if (valid) {
                    axios({
                        method: "POST",
                        url: "/user/register",
                        data: {
                            username: this.formData.username,
                            password: this.formData.password2,
                            email: this.formData.email,
                        }
                    }).then((res)=>{
                        if(res.data){
                            this.$message.success({
                                message: '注册成功',
                                type: 'success'
                            });
                            setTimeout(function () {
                                location.href="../login.html";
                            },1500)
                        }else{
                            this.$message.error('用户已存在，请登录');
                        }
                    });
                } else {
                    this.$message.error('此提交不符合规则');
                    return false;
                }
            });
        },
    }
});